package com.qk.service.impl;

import com.qk.aop.anno.Cacheable;
import com.qk.constant.CacheConstants;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 28188
 */
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取数据总览
     *
     * @return
     */
    @Cacheable(
            keyPrefix = CacheConstants.CACHE_PORTAL_KEY_PREFIX,
            expireTime = 10L,
            expireTimeUnit = TimeUnit.MINUTES)
    @Override
    public OverviewVO getOverview() {
        // 查数据库
        OverviewVO clueOverviewVO = clueMapper.getClueOverviewData();
        OverviewVO businessOverviewVO = businessMapper.getBusinessOverviewData();
        BeanUtils.copyProperties(businessOverviewVO, clueOverviewVO,
                "clueTotal", "clueWaitAllot", "clueWaitFollow",
                "clueFollowing", "clueFalse", "clueConvertBusiness");
        return clueOverviewVO;
    }

    /**
     * 刷新缓存
     */
    @Override
    public void refreshOverviewCache() {
        String redisKey = CacheConstants.CACHE_PORTAL_KEY_PREFIX + "getOverview";
        // 查数据库
        OverviewVO clueOverviewVO = clueMapper.getClueOverviewData();
        OverviewVO businessOverviewVO = businessMapper.getBusinessOverviewData();
        BeanUtils.copyProperties(businessOverviewVO, clueOverviewVO,
                "clueTotal", "clueWaitAllot", "clueWaitFollow",
                "clueFollowing", "clueFalse", "clueConvertBusiness");

        // 缓存结果到redis
        try {
            log.info("查询数据库中的数据: {}, 缓存到redis中", clueOverviewVO);
            redisTemplate.opsForValue().set(redisKey, clueOverviewVO, 10, TimeUnit.MINUTES);
        } catch (Exception e) {
            log.info("缓存数据失败");
            throw new RuntimeException(e);
        }
    }
}

//    public OverviewVO getOverview() {
//
//        String redisKey = CacheConstants.CACHE_PORTAL_KEY_PREFIX + "DATA_OVERVIEW";
//        Object dataOverview;
//
//        // 查redis缓存
//        try {
//            dataOverview = redisTemplate.opsForValue().get(redisKey);
//            if (dataOverview != null) {
//                log.info("查询缓存中的数据: {}", dataOverview);
//                return (OverviewVO) dataOverview;
//            }
//        } catch (Exception e) {
//            log.info("查询缓存中的数据失败, 查询数据库");
//            throw new RuntimeException(e);
//        }
//
//        // 查数据库
//        OverviewVO clueOverviewVO = clueMapper.getClueOverviewData();
//        OverviewVO businessOverviewVO = businessMapper.getBusinessOverviewData();
//        BeanUtils.copyProperties(businessOverviewVO, clueOverviewVO,
//                "clueTotal", "clueWaitAllot", "clueWaitFollow",
//                "clueFollowing", "clueFalse", "clueConvertBusiness");
//
//        // 缓存结果到redis
//        try {
//            log.info("查询数据库中的数据: {}, 缓存到redis中", clueOverviewVO);
//            redisTemplate.opsForValue().set(redisKey, clueOverviewVO, 5, TimeUnit.MINUTES);
//        } catch (Exception e) {
//            log.info("缓存数据失败");
//            throw new RuntimeException(e);
//        }
//        return clueOverviewVO;
//    }
